package com.yenlien.traceyun.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.dao.EquipmentDao;
import com.yenlien.traceyun.business.entity.Equipment;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.business.service.EquipmentService;
import com.yenlien.traceyun.webase.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * Created by LiuSha on 2020/7/28.
 */
@RestController
@RequestMapping(value = "/equipment")
@Api(tags = "设备管理")
public class EquipmentController extends BaseController {
    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private EquipmentDao equipmentDao;

    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody Equipment equipment) {
        equipment.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        return buildPage(equipmentService.findByList(equipment));
    }

//    @RequestMapping(value = "/countAll", method = RequestMethod.GET)
//    public Object countAll(){
//        equipmentService
//    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(@RequestBody Equipment equipment) {
        equipment.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        equipmentService.save(equipment);
        return success("success");
    }


//    @RequestMapping(value = "/update", method = RequestMethod.POST)
//    public Object update(@RequestBody Equipment equipment) {
//        equipmentService.update(equipment);
//        return success("success");
//    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable String id) {
        return success(equipmentService.findById(id));
    }


    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public Object getById(String id) {
        return success(equipmentService.getById(id));
    }


    // 删除设备并且删除地块上的点位信息以及绑定信息
    @ApiOperation(value = "删除设备并且删除地块上的点位信息以及绑定信息, 参数设备ID", notes = "")
    @RequestMapping(value = "/delById", method = RequestMethod.GET)
    public Object delById(String id) {
        equipmentService.delById(id);
        return success("success");
    }

    @ApiOperation(value = "修改设备, 没有修改的字段数据原封不动传上来", notes = "")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object delById(@RequestBody Equipment equipment) {
        equipmentService.update(equipment);
        return success("success");
    }





    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Object delete(@PathVariable String id) {
        equipmentService.delete(id);
        return success("success");
    }

    @RequestMapping(value = "/findByAll", method = RequestMethod.GET)
    public Object findByAll() {
        return success(equipmentService.findAll(UserAuthUtil.getLoginUser().getCompanyId()));
    }

    @RequestMapping(value = "/findByType", method = RequestMethod.POST)
    public Object findByAllAndType(@RequestBody JSONObject jsonObject) {
        return success(equipmentService.findByCompanyIdAndType(UserAuthUtil.getLoginUser().getCompanyId(), jsonObject.getString("type"), jsonObject.getString("name")));
    }

    @PostMapping("/findByWeather")
    public Object findByWeather(@RequestBody JSONObject params) {
        String id = params.getString("id");
        return success(equipmentService.findByWeather(id));
    }

    @PostMapping("/findTime")
    public Object findTime(@RequestBody JSONObject params) {
        Date startTime = params.getDate("startTime");
        Date endTime = params.getDate("endTime");
        String id = params.getString("Id");
        return success(equipmentService.findTime(startTime, endTime, id));
    }

}
